<template>
  <div class="recommendPage">
    <div class="scroll-nav-wrapper" v-show="!hideNav">
      <slot name="left"></slot>
      <div class="scroll-nav">
        <div
          v-for="item in handleredNavs"
          :key="item.key"
          :class="{ active: item.key === current }"
          @click="handlerNavClick(item)"
        >
          {{ item.title }}
        </div>
        <div class="swiper-scrollbar"></div>
      </div>
      <slot name="right"></slot>
    </div>
    <slot></slot>
    <div class="swiper-scroll-page-container">
      <div class="swiper-wrapper">
        <div class="swiper-slide" v-for="item in handleredNavs" :key="item.key">
          <slot :name="item.key"></slot>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import Swiper from 'swiper/js/swiper.js';
import 'swiper/css/swiper.css';

export default {
  name: 'ScrollSwiper',
  data() {
    return {
      mySwiper: null,
    };
  },
  methods: {
    initSwiper() {
      this.mySwiper = new Swiper('.swiper-scroll-page-container', {
        initialSlide: this.handleredNavs.findIndex(
          (item) => item.key === this.modelValue,
        ),
        on: {
          slideChange: this.$_slideChange,
        },
        allowTouchMove: !this.disabled,
        scrollbar: {
          el: '.swiper-scrollbar',
          dragEl: '.swiper-scrollbar-drag',
        },
        autoHeight: this.autoHeight,
      });
    },
    $_slideChange() {
      if (this.mySwiper) {
        console.log('this.mySwiper', this.mySwiper);
        this.$emit(
          'update:modelValue',
          this.handleredNavs[this.mySwiper.activeIndex].key,
        );
      }
    },
    handlerNavClick(item) {
      if (!this.disabled) {
        this.$emit('update:modelValue', item.key);
      }
    },
  },
  props: {
    modelValue: {
      type: [Number, String],
    },
    navs: {
      type: Array,
      default() {
        return [];
      },
    },
    disabled: {
      type: Boolean,
      default: false,
    },
    hideNav: {
      type: Boolean,
      default: false,
    },
    autoHeight: {
      type: Boolean,
      default: false,
    },
  },
  watch: {
    modelValue: {
      immediate: true,
      handler(val) {
        this.$nextTick(() => {
          this.mySwiper && this.mySwiper.slideTo(val);
        });
      },
    },
  },
  computed: {
    current: {
      set(val) {
        this.$emit('update:modelValue', val);
      },
      get() {
        const index = this.handleredNavs.findIndex(
          (item) => item.key === this.modelValue,
        );
        if (this.mySwiper && index !== this.mySwiper.activeIndex) {
          this.mySwiper.slideTo(index);
        }
        return this.modelValue;
      },
    },
    handleredNavs() {
      return this.navs.map(({ key, ...item }, index) => ({
        ...item,
        key: key === undefined ? index : key,
      }));
    },
  },
  mounted() {
    // this.swiper.slideTo(3, 1000, false);
    this.initSwiper();
  },
};
</script>
<style scoped lang="scss">
.recommendPage .swiper-scroll-page-container {
  position: relative;
  width: 100%;
  height: calc(100vh - 100px);
  overflow-x: hidden;
  overflow-y: auto;
  /* background: pink; */
}
.recommendPage .swiper-scroll-page-container .swiper-slide {
  width: 100%;
  height: 100%;
  /* background: yellowgreen; */
  color: #000;
  font-size: 16px;
  text-align: center;
}

.scroll-nav-wrapper {
  display: flex;
  background: rgba(255, 255, 255, 1);
}

.scroll-nav {
  flex: 1;
  position: relative;
  display: flex;
  line-height: 44px;
  text-align: center;
  font-size: 16px;
  color: rgba(51, 51, 51, 1);
  & > div {
    flex: 1;
    &.active {
      color: #f84848;
      transition: color 0.3s;
    }
  }
}
.swiper-scrollbar {
  position: absolute;
  left: 0;
  bottom: 5px;
  width: 100%;
  height: 5px;
  background: transparent;
  ::v-deep .swiper-scrollbar-drag {
    background: transparent;
    position: relative;
    &:after {
      content: "";
      position: absolute;
      width: 20px;
      height: 5px;
      top: 0;
      left: 50%;
      margin-left: -10px;
      background: url("")
        no-repeat center center;
      background-size: contain;
    }
  }
}
</style>
